package 富婆梦.第一阶段.第八周Scoket.作业1;

import java.io.*;
import java.util.Properties;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第八周Scoket.作业1
 * @ClassName: DeviceManage
 * @Author: hemengjin
 * @Description: 操作device对象的类，提供存储device对象、读取device对象的两个方法,
 *               使用properties格式的文件进行信息的读取和保存
 * @Date:2021/9/24 15:38
 * @Version: 1.0
 */
public class DeviceManage1 {

    //存储device对象至properties格式文件中的方法
    public void saveDevice(Device device) {
        //创建文件来保存device对象
        File f = new File("");
        String path = f.getAbsolutePath() + "\\src\\富婆梦\\第一阶段\\第八周Scoket\\作业1\\device.properties";
        File file = new File(path);

        String deviceid = String.valueOf(device.deviceID);
        String bindStatus = String.valueOf(device.bindStatus);
        String workType = String.valueOf(device.workType);
        String s1_name = device.s1.switchName;
        String s2_name = device.s2.switchName;
        String s3_name = device.s3.switchName;
        String s4_name = device.s4.switchName;
        String s1_num = String.valueOf(device.s1.switchNumber);
        String s2_num = String.valueOf(device.s2.switchNumber);
        String s3_num = String.valueOf(device.s3.switchNumber);
        String s4_num = String.valueOf(device.s4.switchNumber);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            Properties properties = new Properties();
            properties.setProperty("deviceID", deviceid);
            properties.setProperty("bindStatus", bindStatus);
            properties.setProperty("workType", workType);
            properties.setProperty("s1_name", s1_name);
            properties.setProperty("s2_name", s2_name);
            properties.setProperty("s3_name", s3_name);
            properties.setProperty("s4_name", s4_name);
            properties.setProperty("s1_num", s1_num);
            properties.setProperty("s2_num", s2_num);
            properties.setProperty("s3_num", s3_num);
            properties.setProperty("s4_num", s4_num);
            properties.store(fileOutputStream, "");
            System.out.println("存储成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从properties文件中读取device对象的方法
    public Device loadDevice(String deviceId) {
        File f = new File("");
        String path = f.getAbsolutePath() + "\\src\\富婆梦\\第一阶段\\第八周Scoket\\作业1\\device.properties";
        File file = new File(path);
        Properties properties = new Properties();

        try {
            InputStream in = new FileInputStream(file);
            properties.load(in);
            if (properties.getProperty("deviceID").equals(deviceId)) {
                long deviceID = Long.valueOf(properties.getProperty("deviceID"));
                int bindStatus = Integer.valueOf(properties.getProperty("bindStatus"));
                short workType = Short.valueOf(properties.getProperty("workType"));
                Switch s1 = new Switch(properties.getProperty("s1_name"), Integer.valueOf(properties.getProperty("s1_num")));
                Switch s2 = new Switch(properties.getProperty("s2_name"), Integer.valueOf(properties.getProperty("s2_num")));
                Switch s3 = new Switch(properties.getProperty("s3_name"), Integer.valueOf(properties.getProperty("s3_num")));
                Switch s4 = new Switch(properties.getProperty("s4_name"), Integer.valueOf(properties.getProperty("s4_num")));
                Device device = new Device(deviceID, bindStatus, workType, s1, s2, s3, s4);
                System.out.println("读取成功！");
                return device;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        DeviceManage1 deviceManage = new DeviceManage1();
        Device device = new Device();
        deviceManage.saveDevice(device);
        Device getDevice = deviceManage.loadDevice("20000001");
        System.out.println("deviceID:" + getDevice.deviceID);
        System.out.println("bindStatus:" + getDevice.bindStatus);
        System.out.println("workType:" + getDevice.workType);
        System.out.println("s1_name:" + getDevice.s1.switchName);
        System.out.println("s2_name:" + getDevice.s2.switchName);
        System.out.println("s3_name:" + getDevice.s3.switchName);
        System.out.println("s4_name:" + getDevice.s4.switchName);
    }
}

